import { createId } from "@paralleldrive/cuid2";
import { type InferInsertModel, type InferSelectModel, type SQL, sql } from "drizzle-orm";
import { bigint, integer, json, pgTable, varchar } from "drizzle-orm/pg-core";
import { createInsertSchema, createSelectSchema } from "drizzle-zod";
import type { Summary } from "../types/allure";

export const reports = pgTable("reports", {
  id: varchar({ length: 255 })
    .primaryKey()
    .$defaultFn(() => createId()),
  name: varchar({ length: 255 }).notNull(),
  number: integer().notNull(),
  summary: json().$type<Summary>().notNull(),
  start: bigint({ mode: "number" })
    .generatedAlwaysAs((): SQL => sql`(${reports.summary}->'time'->>'start')::bigint`)
    .notNull(),
});

export const ReportsInsertSchema = createInsertSchema(reports);
export const ReportsSelectSchema = createSelectSchema(reports);
export type Report = InferSelectModel<typeof reports>;
export type ReportInsert = InferInsertModel<typeof reports>;
